//
//  TourMapVC.m
//  ImGuider
//
//  Created by llt on 2017/7/5.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "TourMapVC.h"
#import "TourMapCell.h"
#import "LinePreDetailVC.h"
#import "MapMerchantCell.h"
#import "RecommendMerchantDetailVC.h"
#import "IGAvPlayerManager.h"

@interface TourMapVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation TourMapVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 0) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"TourMapCell" bundle:nil] forCellReuseIdentifier:@"TourMapCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MapMerchantCell" bundle:nil] forCellReuseIdentifier:@"MapMerchantCell"];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    UIButton *hiddenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [hiddenBtn addTarget:self action:@selector(hideSelf) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:hiddenBtn];
    
    [hiddenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
        make.bottom.equalTo(self.tableView.mas_top);
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if ([self.model.playpath isEqualToString:[IGAvPlayerManager shareManeger].currentURL]) {
        [[IGAvPlayerManager shareManeger] stop];
    }
}

- (void)show:(UIViewController *)vc {
    
    
    [vc addChildViewController:self];
    [vc.view addSubview:self.view];
    
    CGRect desFrame = vc.view.frame;
    
    desFrame.origin.y = kScreenHeight;
    self.tableView.frame = desFrame;
    
    @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        
        @strongify(self);
        self.tableView.frame = vc.view.frame;
        
    }];
}

- (void)hideSelf {
    
    CGRect desFrame = self.tableView.frame;
    desFrame.origin.y = kScreenHeight;
    
    @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        
        @strongify(self);
        self.tableView.frame = desFrame;
        
    } completion:^(BOOL finished) {
        
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

- (void)setModel:(MerchantModel *)model {
    
    _model = model;
    CGFloat tbHeight = 110;
    
    self.tableView.frame = CGRectMake(16, kScreenHeight - tbHeight, kScreenWidth - 32, tbHeight);
    [self.tableView reloadData];
}

- (void)setLineArray:(NSArray *)lineArray {
    
    _lineArray = lineArray;
    
    CGFloat tbHeight = (self.lineArray.count > 4 ? 4.3 : self.lineArray.count) * 60;
    
    self.tableView.frame = CGRectMake(16, kScreenHeight - tbHeight - kBottomHeight, kScreenWidth - 32, tbHeight);
    [self.tableView reloadData];
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model == nil ? self.lineArray.count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.model) {
        MapMerchantCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MapMerchantCell"];
        cell.model = self.model;
        
        return cell;
    }
    
    TourMapCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TourMapCell"];
    
    LineModel *line = self.lineArray[indexPath.row];
    
    cell.lineModel = line;
    
    return cell;
    
    
}
#pragma mark -UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.model) {
        
        RecommendMerchantDetailVC * vc = [[RecommendMerchantDetailVC alloc] init];
        
        [self.navigationController pushViewController:vc animated:YES];
        vc.model = self.model;
        return;
    }
    
    LineModel *model = self.lineArray[indexPath.row];
    LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = model.lineid;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end



